//
//  HLVisitorView.swift
//  MyBlog
//
//  Created by 李芙蓉 on 16/3/4.
//  Copyright © 2016年 hanlei. All rights reserved.
//

import UIKit
import SnapKit

class HLVisitorView: UIView {
    
    //懒加载图片
    var headImageView : UIImageView = {
        
        let h = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return h
    }()
    
    //懒加载描述
    var descriptionLabel : UILabel = {
    
        let d = UILabel()
        
        d.textColor = myTextColor
        
        d.numberOfLines = 0
        
        d.textAlignment = .Center
        
        d.text = "visitordiscover_feed_image_house"
        
        return d
    }()
    
    //懒加载环绕图片
    var circleImageView : UIImageView = {
    
        let c = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return c
    }()
    
    //懒加载登录按钮
    var loginButton : UIButton = {
        
        let l = UIButton(title: "登录", backgroundImageName: "common_button_white")
        
        return l
    }()
    
    //懒加载注册按钮
    var registerButton : UIButton = {
    
        let r = UIButton(title: "注册", backgroundImageName: "common_button_white")
        
        return r
    }()
    
    //懒加载覆盖图片
    var coverImageView : UIImageView = {
        
        let c = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return c
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 235/255.0, alpha: 1.0)
        
        addSubview(descriptionLabel)
        
        addSubview(circleImageView)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        addSubview(coverImageView)
        
        addSubview(headImageView)
        
        //给headImageView添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //给descriptionLabel添加约束
        descriptionLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            
            make.top.equalTo(headImageView.snp_bottom).offset(40)
        }
        
        //给circleImageView添加约束
        circleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(headImageView)
        }
        
        //给loginButton添加约束
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(circleImageView)
            
            make.top.equalTo(descriptionLabel.snp_bottom).offset(20)
            
            make.width.equalTo(60)
        }
        
        //给registerButton添加约束
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(circleImageView)
            
            make.top.equalTo(loginButton)
            
            make.width.equalTo(loginButton)
        }
        
        //给coverImageView添加约束
        coverImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(circleImageView).offset(-10)
            
            make.centerX.equalTo(circleImageView)
        }
    }
    
    convenience init(headImageName:String?=nil, description:String?=nil, frame:CGRect) {
        
        self.init()
        
        if headImageName == nil {
            
            circleImageView.layer.addAnimation(circleAnimation(), forKey: nil)
            
        } else {
            
            circleImageView.hidden = true
        }
        
        if let imageName = headImageName {
            
            headImageView.image = UIImage(named: imageName)
        }
        
        if let text = description {
            
            descriptionLabel.text = text
        }
    }
    
    //circleImageView的动画方法
    private func circleAnimation () -> CABasicAnimation {
        
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        
        basicAnimation.removedOnCompletion = false
        
        basicAnimation.repeatCount = MAXFLOAT
        
        basicAnimation.duration = 10
        
        basicAnimation.toValue = 2 * M_PI
        
        return basicAnimation
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
